#include "global.h"
#include <time.h>
#include <pthread.h>

#include <stdio.h>
#include <string.h>
/*
SteelNzb
Copyright (C) 2008  Wessel v. Staal

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <stdlib.h>


#ifdef WIN32 
#include <windows.h>
#ifndef WINCE
#include <direct.h>
#endif
#else
#include <unistd.h>
#endif

typedef struct statsInfo
{
	int fileCount;
	int processedFiles;
	int failedArticles;
	int bytesDownloaded;
	time_t downloadedTime; 
	char currentNzbFilename[1024];
	int isPaused;
	int speed;
	pthread_mutex_t statsMutex;
}statsInfo;

#ifdef WINCE
time_t time( time_t *inTT )
{
SYSTEMTIME sysTimeStruct;
FILETIME fTime;
ULARGE_INTEGER int64time;
time_t locTT = 0;

if ( inTT == NULL ) {
inTT = &locTT;
}

GetSystemTime( &sysTimeStruct );
if ( SystemTimeToFileTime( &sysTimeStruct, &fTime ) ) {
memcpy( &int64time, &fTime, sizeof( FILETIME ) );
/* Subtract the value for 1970-01-01 00:00 (UTC) */
int64time.QuadPart -= 0x19db1ded53e8000;
/* Convert to seconds. */
int64time.QuadPart /= 10000000;
*inTT = (time_t)int64time.QuadPart;
}
return *inTT;
}
#endif

void* stats_createStats()
{
	statsInfo* stats = NULL;

	if ( (stats = malloc(sizeof(statsInfo))) == NULL )
	{
		perror("Stats::CreateStatsInfo");
		exit(1);
	}

	memset(stats, 0, sizeof(statsInfo));

	
	if ( pthread_mutex_init(&stats->statsMutex, NULL) != 0 )
	{
		PRINTERROR("Stats::CreateMutex");
		free(stats);
		return 0;
	}

	return stats;
}

void stats_freeStats(void* stats)
{
	free(stats);
}

void stats_setFileCount(void* stats, int value)
{
	statsInfo* statsinfo = stats;

	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->fileCount = value;

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

long stats_getFileCount(void* stats)
{
	statsInfo* statsinfo = stats;
	int value = 0 ;

	pthread_mutex_lock(&statsinfo->statsMutex);

	value = statsinfo->fileCount;

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return value;
}

void stats_addProcessed(void* stats, int value)
{
	statsInfo* statsinfo = stats;

	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->processedFiles += value;

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

void stats_setProcessed(void* stats, int value)
{
	statsInfo* statsinfo = stats;

	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->processedFiles = value;

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

long stats_getProcessed(void* stats)
{
	statsInfo* statsinfo = stats;
	int value = 0 ;

	pthread_mutex_lock(&statsinfo->statsMutex);

	value = statsinfo->processedFiles;

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return value;
}

void stats_addFailedCount(void* stats, int value)
{
	statsInfo* statsinfo = stats;

	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->failedArticles += value;

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

void stats_setFailedCount(void* stats, int value)
{
	statsInfo* statsinfo = stats;

	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->failedArticles = value;

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

int stats_getFailedCount(void* stats)
{
	statsInfo* statsinfo = stats;
	long value = 0 ;

	pthread_mutex_lock(&statsinfo->statsMutex);

	value = statsinfo->failedArticles;

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return value;
}

void stats_setCurrentNzbFile(void* stats, char* value)
{
	statsInfo* statsinfo = stats;

	pthread_mutex_lock(&statsinfo->statsMutex);

	if ( value != NULL && strlen(value) < sizeof(statsinfo->currentNzbFilename) )
		strcpy(statsinfo->currentNzbFilename, value);

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

char* stats_getCurrentNzbFile(void* stats, char* dest)
{
	statsInfo* statsinfo = stats;

	pthread_mutex_lock(&statsinfo->statsMutex);

	strcpy(dest, statsinfo->currentNzbFilename);

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return dest;
}

int stats_getDownloadSpeed(void* stats)
{
	statsInfo* statsinfo = stats;
	int value = 0 ;

	pthread_mutex_lock(&statsinfo->statsMutex);

	value = statsinfo->speed;

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return (int)value;
}

int stats_getProgress(void* stats)
{
	statsInfo* statsinfo = stats;
	int ret = 0;

	pthread_mutex_lock(&statsinfo->statsMutex);

	if ( statsinfo->fileCount != 0 )
	{
		ret = (int)( (double)statsinfo->processedFiles / (double)statsinfo->fileCount * 100.0 );
	}

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return ret;
}

int stats_getIsPaused(void* stats)
{
	statsInfo* statsinfo = stats;
	int ret = 0;

	pthread_mutex_lock(&statsinfo->statsMutex);

	ret = statsinfo->isPaused ;

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return ret;
}

void stats_setIsPaused(void* stats, int paused)
{
	statsInfo* statsinfo = stats;
	
	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->isPaused = paused;

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

int stats_getBytesDownloaded(void* stats)
{
	statsInfo* statsinfo = stats;
	int value = 0 ;

	pthread_mutex_lock(&statsinfo->statsMutex);

	value = statsinfo->bytesDownloaded;

	pthread_mutex_unlock(&statsinfo->statsMutex);

	return value;
}

void stats_addBytesDownloaded(void* stats, int value)
{
	statsInfo* statsinfo = stats;
	time_t timeval = 0;
	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->bytesDownloaded += value;

	time(&timeval);

	timeval = timeval - statsinfo->downloadedTime;
	
	if ( timeval > 0 )
	{
		statsinfo->speed = (int)((double)statsinfo->bytesDownloaded / (double)timeval);
		statsinfo->downloadedTime = time(&timeval);
		statsinfo->bytesDownloaded = 0;
	}

	pthread_mutex_unlock(&statsinfo->statsMutex);
}

void stats_setBytesDownloaded(void* stats, int value)
{
	statsInfo* statsinfo = stats;
	
	pthread_mutex_lock(&statsinfo->statsMutex);

	statsinfo->bytesDownloaded = value;

	pthread_mutex_unlock(&statsinfo->statsMutex);
}
